package com.cction.activity.reinterest

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager

import com.cction.R
import com.cction.comm.SaveCompoundUtils
import com.cction.entry.CctionBean
import com.umeng.analytics.MobclickAgent
import com.zhy.adapter.recyclerview.CommonAdapter
import com.zhy.adapter.recyclerview.base.ViewHolder
import kotlinx.android.synthetic.main.recordlist_main.*
import kotlinx.android.synthetic.main.title_bar.*

/**
 * 复利列表
 */
class CompoundListActivity : AppCompatActivity(), View.OnClickListener {
    private val context = this
    private var arraylist: ArrayList<CctionBean>? = null
    private var adapter: CommonAdapter<CctionBean>? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.recordlist_main)

        init()
    }

    private fun init() {
        title_bar_title.text = "记录"
        title_bar_other.text = "清空"
        title_bar_back.setOnClickListener(this)
        title_bar_other.setOnClickListener(this)

        arraylist = ArrayList()
        record_list_recycler.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
        adapter = object : CommonAdapter<CctionBean>(context, R.layout.record_item, arraylist) {
            override fun convert(holder: ViewHolder, cctionBean: CctionBean, position: Int) {

                holder.setText(R.id.record_item_time, "记录时间：${cctionBean.ctime}")
                when (cctionBean.type) {
                    1 -> {
                        holder.setText(R.id.record_item_title, "复利")
                        holder.setBackgroundRes(R.id.record_item_layout, R.color.orange)
                        holder.setText(R.id.record_item_principal, "复利现值：${cctionBean.principal}")
                        holder.setText(R.id.record_item_result, "复利终值：${cctionBean.result}")
                    }
                    2 -> {
                        holder.setText(R.id.record_item_title, "本金")
                        holder.setBackgroundRes(R.id.record_item_layout, R.color.blue)
                        holder.setText(R.id.record_item_principal, "复利现值：${cctionBean.principal}")
                        holder.setText(R.id.record_item_result, "复利终值：${cctionBean.result}")
                    }
                    3 -> {
                        holder.setText(R.id.record_item_title, "定投")
                        holder.setBackgroundRes(R.id.record_item_layout, R.color.green)
                        holder.setText(R.id.record_item_principal, "每次定投：${cctionBean.principal}")
                        holder.setText(R.id.record_item_result, "定投终值：${cctionBean.result}")
                    }
                    4 -> {
                        holder.setText(R.id.record_item_title, "本利")
                        holder.setBackgroundRes(R.id.record_item_layout, R.color.lightgreen)
                        holder.setText(R.id.record_item_principal, "每次定投：${cctionBean.principal}")
                        holder.setText(R.id.record_item_result, "定投终值：${cctionBean.result}")
                    }
                }
                holder.itemView.setOnClickListener {
                    val intent = Intent()
                    intent.setClass(context, CCtionDetailActivity::class.java)
                    val bundle = Bundle()
                    bundle.putSerializable("time", cctionBean)
                    intent.putExtras(bundle)
                    startActivity(intent)
                }
            }
        }
        record_list_recycler.adapter = adapter
    }

    override fun onClick(view: View) {
        when (view.id) {
            R.id.title_bar_back -> finish()
            R.id.title_bar_other -> {
                val builder = AlertDialog.Builder(context)
                builder.setTitle("提示")
                builder.setMessage("确定要清空所有记录吗")
                builder.setPositiveButton("取消") { _, _ -> }
                builder.setNegativeButton("确定") { _, _ ->
                    SaveCompoundUtils.clear(context)
                    initData()
                }
                builder.show()
            }
        }
    }

    /**
     * 数据更新
     */
    private fun initData() {
        arraylist?.clear()
        arraylist?.addAll(SaveCompoundUtils.readData(context).arrayList)
        if (arraylist?.size == 0) {
            record_item_empty.visibility = View.VISIBLE
            title_bar_other.visibility = View.GONE
        } else {
            record_item_empty.visibility = View.GONE
            title_bar_other.visibility = View.VISIBLE
        }
        adapter?.notifyDataSetChanged()
    }

    override fun onResume() {
        super.onResume()
        initData()
        MobclickAgent.onResume(this)
    }


    public override fun onPause() {
        super.onPause()
        MobclickAgent.onPause(this)
    }
}
